package com.lanou3g.hw0518.listener;

import com.lanou3g.hw0518.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Set;

@WebListener
public class SessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//相当于 退出登录
        HttpSession session = httpSessionEvent.getSession();
        String username = ((User)session.getAttribute("user")).getUsername();
        ServletContext app = session.getServletContext();
        Set<String>loginUsers = (Set<String>) app.getAttribute("loginUser");
        loginUsers.remove(username);
}}
